package com.dissertation.action;

import com.dissertation.pojo.Message;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    /**
     * 登陆
     * @return
     */

    @RequestMapping("/sign")
    public String sign() {
        return "html/signin";
    }

    @RequestMapping("/logOut")
    @ResponseBody
    public Message logOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Message.createSuc( "login" );
    }

    @PostMapping( value = "login" )
    @ResponseBody
    public Message login( String username, String password ) {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken( username, password );
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login( usernamePasswordToken );   //完成登录
//            User user = (User) subject.getPrincipal();
            return Message.createSuc( true );
        } catch ( Exception e ) {
            e.printStackTrace();
            return Message.createErr( e.getMessage() );//返回登录页面
        }
    }

}
